package org.whb.org.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.whb.basic.util.AjaxResult;
import org.whb.basic.util.PageList;
import org.whb.org.domain.SystemDictionaryDetails;
import org.whb.org.domain.SystemDictionaryType;
import org.whb.org.query.SystemDictionaryDetailsQuery;
import org.whb.org.service.ISystemDictionaryDetailsService;

import java.util.List;

@RestController
@RequestMapping("/dicDetails")
public class SystemDictionaryDetailsController {

    @Autowired
    private ISystemDictionaryDetailsService systemDictionaryDetailsService;


    @PostMapping("/list")
    public PageList<SystemDictionaryDetails> queryPage(@RequestBody SystemDictionaryDetailsQuery query){
        return systemDictionaryDetailsService.queryPage(query);
    }

    @PutMapping
    public AjaxResult addorSystemDictionaryType(@RequestBody SystemDictionaryDetails systemDictionaryDetails){
        try {
            if(systemDictionaryDetails.getId()==null){//表示新增
                systemDictionaryDetailsService.add(systemDictionaryDetails);
            }else {//表示修改
                systemDictionaryDetailsService.update(systemDictionaryDetails);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("操作失败");
        }
    }

    @DeleteMapping("{id}")
    public AjaxResult removeDept(@PathVariable Long id){
        try {
            systemDictionaryDetailsService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids 批量删除的id
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            systemDictionaryDetailsService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("删除失败");
        }



    }


    @PostMapping
    public List<SystemDictionaryDetails> queryById(@RequestBody SystemDictionaryType type){
        return systemDictionaryDetailsService.queryManyById(type);
    }

}
